﻿using System;
using System.Collections.Generic;
using WAD.WEB.AppCode;
using WAD.DAL;
using WAD.Model;
using System.Text;

namespace WAD.WEB.Passport.ajax
{
    /// <summary>
    /// FC 的摘要说明
    /// </summary>
    public class FC : PassportAjax
    {
        public override void AjaxMethodDispatch(String identity)
        {
            switch (identity)
            {
                case "getrecordlist": this.GetRecordList(); break;
                case "gettotal": this.GetTotal(); break;
                case "getclientlist": this.GetClientList(); break;
            }
        }
        /// <summary>
        /// 代理商返利列表
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        void GetRecordList()
        {
            int uid = Parse.Int("uid", 0);      //客户ID
            int page = Parse.Int("page", 1);
            int modelid = Parse.Int("modelid", 2);   //2 代理商返利，3运营商返利
            string nick_name = Parse.String("sitename");
            if (UserId < 1)
                ParamError();

            StringBuilder sb = new StringBuilder();
            sb.Append("user_id=" + UserId);
            if (uid > 0)
                sb.AppendFormat(" and by_user_id={0}", uid);

            if (!string.IsNullOrEmpty(nick_name))
                sb.AppendFormat(" and by_user_id in(select [user].id from [user] where [user].agentid={0} and [user].nick_name like '%{1}%')", UserId, QD.WX.Utill.Util.FilterSql(nick_name));

            PageData dt = DAO.GetListPage("id,by_user_id,title,op_total,op_price,op_remainder,rebate_state,id as articleid,'' as nick_name,'' as discount", "id desc"
                , page, 20, sb.ToString(), "v_fc_article#user##fanli");
            if (dt.table.Rows.Count == 0)
                DataEmpty();

            //折扣
            Dictionary<string, object> agent = DaoUser.GetDetail(UserId, "nick_name,discount,yunying_discount");
            if (agent != null)
            {
                object discount = agent["discount"];
                if (modelid == 3)
                    discount = agent["yunying_discount"];
                for (int i = 0; i < dt.table.Rows.Count; i++)
                {
                    dt.table.Rows[i]["discount"] = discount;
                    dt.table.Rows[i]["nick_name"] = agent["nick_name"];
                }
            }

            JsonResult(0, "", dt);
        }

        [QD.Web.Ajax.AjaxMethod]
        void GetTotal()
        {
            int uid = Parse.Int("uid", 0);
            string nick_name = Parse.String("sitename");
            int modelid = Parse.Int("modelid", 2);   //2 代理商返利，3运营商返利
            if (modelid < 2 || modelid > 3)
                DataEmpty();
            Success(DAOFCRecord.GetTotal_FanLi(UserId, uid, nick_name, modelid));
        }
     
        /// <summary>
        /// 发财客户列表
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        void GetClientList()
        {
            int uid = Parse.Int("uid", 0);
            int page = Parse.Int("page", 1);
            string nick_name = Parse.String("sitename");

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" agentid={0}", UserId);
            if (uid > 0)
                sb.AppendFormat(" and id={0}", uid);
           
            if (!string.IsNullOrEmpty(nick_name))
                sb.AppendFormat(" and nick_name like '%{0}%'", QD.WX.Utill.Util.FilterSql(nick_name));

            PageData dt = DAO.GetListPage("id,nick_name,phone,state,date", "id desc", page, 20, sb.ToString(), "[user]");
            JsonResult(0, "", dt);
        }
    }
}